package com.xr50.video.controller;

import com.xr50.common.SaveRequest;
import com.xr50.video.service.UserServerHttp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class VideoController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserServerHttp userServerHttp;

    @GetMapping("is/who")
    public String idWho() {
        return "我是 video ";
    }



    @GetMapping("get/user")
    public String getUser() {
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//
//        ServiceInstance serviceInstance = instances.get(0);
//        String host = serviceInstance.getHost();
//        int port = serviceInstance.getPort();
        String isWho = userServerHttp.getIsWho();
//        String s = restTemplate.getForObject("http://user-service/is/who", String.class);

        return isWho;
    }



    @PostMapping("save")
    public  String save(@RequestBody SaveRequest name){
        String result = userServerHttp.save(name);

        return "video   我保存了"+  result;

    }

}
